<?php
include_once "lib/config.php";
if(!isset($_REQUEST['cpid']) || $_REQUEST['cpid']<1)
{
	header("location: company_pages.php");
}
include_once "header.php";

if(isset($_POST['cpid']) && $_POST['cpid']>0 && isset($_POST['opt_status']) && $_POST['opt_status']!="")
{
	//echo "<pre>";
//	print_r($_POST);
//	die("vikas testing");
	
	if($_POST['opt_status']=="2")
    {
        $recdetail=DB::fetch_array(DB::query("select Comp_Page_Name from af_company_page where Comp_Page_ID='".$_POST['cpid']."'"));
        $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Deleted the Media of Company Page ".$recdetail[0]['Comp_Page_Name'],"logtime"=>time());
        DB::insertData("stafflog",$insertdataarr);
        
        $arr_sanitize=array();
        $arr_sanitize["status"]="2";
        $arr_sanitize["delete_date"]=time();
        DB::updateData("af_company_page_media",$arr_sanitize,array("Media_Upload_ID"=>$_POST['Media_Upload_ID']));
        
        $msg_success="Company Page Media Deleted successfully.";
        
    }
    else
    {
    	$arr_sanitize=array("status"=>$_POST['opt_status']);
        $recdetail=DB::fetch_array(DB::query("select Comp_Page_Name from af_company_page where Comp_Page_ID='".$_POST['cpid']."'"));
        
        DB::updateData("af_company_page_media",$arr_sanitize,array("Media_Upload_ID"=>$_POST['Media_Upload_ID']));
    	
        if($_POST['opt_status']=="0")
    	{
    	    $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Activated the Media of Company Page ".$recdetail[0]['Comp_Page_Name'],"logtime"=>time());
    		$msg_success="Company Page Media enabled/activated successfully.";
    	}
    	if($_POST['opt_status']=="1")
    	{
    	    $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Deactivated the Media of Company Page ".$recdetail[0]['Comp_Page_Name'],"logtime"=>time());
    		$msg_success="Company Page Media Deactivated successfully.";
    	}
        
        DB::insertData("stafflog",$insertdataarr);
        
     }
}


if(isset($_SESSION['msg_success']) && $_SESSION['msg_success']!="")
{
	$msg_success=$_SESSION['msg_success'];
	unset($_SESSION['msg_success']);
}

if(isset($_SESSION['msg_error']) && $_SESSION['msg_error']!="")
{
	$msg_error=$_SESSION['msg_error'];
	unset($_SESSION['msg_error']);
}

if(!isset($_POST['order_by']))
{
	$_POST['order_by']="desc";
}

if(!isset($_POST['type_title']))
{
	$_POST['type_title']="updated_date";
}

if(!isset($_POST['pid']))	// code starts for implementing pagination 
{
	$_POST['pid']=1;
}

$conditionarr=array(":Comp_Page_ID"=>$_REQUEST['cpid']);

$pageinfo=DB::fetch_array(DB::query("select * from af_company_page where Comp_Page_ID=:Comp_Page_ID and Status!=2",$conditionarr));

$selectquery="select af_company_page_media.* from  af_company_page_media where Status !=2 and af_company_page_media.Comp_Page_ID=:Comp_Page_ID";


if(isset($_POST['searchText']) && $_POST['searchText']!="" && isset($_POST['filterby']) && $_POST['filterby']!="")
{
    $selectquery.=" and ".$_POST['filterby']." like :".$_POST['filterby'];
	$conditionarr[":".$_POST['filterby']]= "%".$_POST['searchText']."%";
}

$totalrecords=DB::fetch_num_of_rows(DB::query($selectquery,$conditionarr));

$selectquery.=" order by ".$_POST['type_title']." ".$_POST['order_by']." limit ".(($_POST['pid']-1)*PAGELIMIT).", ".PAGELIMIT;

$dbinfo=DB::fetch_array(DB::query($selectquery,$conditionarr));

include_once "lib/paginate_class.php";

$paging_obj=new Paginate();

$pagingdiv=$paging_obj->getnewpager($_POST['pid'],$totalrecords);

//echo "<pre>";
//print_r($dbinfo);
//echo "</pre>";
//
?>
<script type="text/javascript" src="../js/fancybox/jquery.fancybox.js?v=2.1.5"></script> 
<link rel="stylesheet" type="text/css" href="../js/fancybox/jquery.fancybox.css?v=2.1.5" media="screen" />        
		<div class="container_12">
        
            <div class="bottom-spacing">

                    <form name="mainform" method="post">
	                    <input type="hidden" name="Media_Upload_ID" id="Media_Upload_ID">
	                    <input type="hidden" name="opt_status" id="opt_status">
						<input type="hidden" name="pid" id="pid" value="<?php if(isset($_POST['pid']))echo $_POST['pid']; ?>">
						<input type="hidden" name="type_title" id="type_title" value="<?php if(isset($_POST['type_title']))echo $_POST['type_title']; ?>">
						<input type="hidden" name="order_by" id="order_by" value="<?php if(isset($_POST['order_by']))echo $_POST['order_by']; ?>">
						<input type="hidden" name="cpid" id="cpid" value="<?php echo $_REQUEST["cpid"]; ?>">
						
					</form>
                	<br>
            </div>
            
            
                <!-- Example table -->
                <div class="module" style="width:100% !important;">
                	<h2><span><?php if(count($pageinfo)>0 && isset($pageinfo[0]["Comp_Page_Name"])) { echo $pageinfo[0]["Comp_Page_Name"]."'s"; }  ?> Members List</span></h2>
                    
                    <div class="module-table-body">
                    	
                        <table id="myTable" >
                        	<thead>
                                <tr>
                                    <th style="width:10%">Sr.No.</th>
                                    <th style="width:40%"><a href="javascript:void(0)"  >Media</a></th>
                                    <th style="width:10%"><a href="javascript:void(0)" onclick="return change_order('updated_date','<?php echo $_POST['order_by'] ?>')" >Upload Date</a></th>
                                    <th style="width:18%">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
                            if(count($dbinfo)>0)
                            {
                            	$count=0;
                            	for($i=0;$i<count($dbinfo);$i++)
                            	{
                            		$count++;
                            ?>
							    <tr <?php if($count%2==0)echo "class='odd'"; else echo "class='even'"; ?>>
                                    <td class="align-center"><?php echo ($i+1);  ?></td>
                                    <td>
                                    <?php
                                    	if($dbinfo[$i]["Media_Type"]=="IMAGE")
                                    	{
									?>
										<a href="../<?php echo $dbinfo[$i]["Media_Path"]; ?>" class="PagePhotoGallery" data-fancybox-group="PagePhotoGallery"><img src="../<?php echo "uploads/image_crop.php?image=/".$dbinfo[$i]["Media_Path"]."&width=93&height=91&cropratio=93:91";  ?>" /></a>
									<?php		
										}
                                    ?>
									</td>
                                    <td><?php echo date("d/m/Y",$dbinfo[$i]["updated_date"]); ?></td>
                                    <td>
                                    	<?php if($dbinfo[$i]['Status']=="1" ){  ?><a href="javascript:void(0);" onclick="return performoperation('Activate the Media','0',<?php echo $dbinfo[$i]['Media_Upload_ID']; ?>,'Media_Upload_ID');"><img src="images/tick-circle.gif" width="16" height="16" title="Activate" /></a><?php }else{ ?>
                                    	<a href="javascript:void(0);" onclick="return performoperation('de-activate/disable the Media','1',<?php echo $dbinfo[$i]['Media_Upload_ID']; ?>,'Media_Upload_ID');"><img src="images/minus-circle.gif" width="16" height="16" title="Deactivate" /></a><?php } ?>
                                    	
                                        <a href="javascript:void(0);" onclick="return performoperation('delete the Media','2',<?php echo $dbinfo[$i]['Media_Upload_ID']; ?>,'Media_Upload_ID');"><img src="images/bin.gif" width="16" height="16" title="Delete" /></a>
                                        
                                    </td>
                                </tr>
                            <?php
                            	}
                            }
                            else
                            {
								?>
								<tr>
									<td colspan="9"><span class="redspan">No members joined this page.</span></td>
								</tr>
								<?php	
							}
                            ?>
                            </tbody>
                        </table>
                        <br>
                        <?php echo $pagingdiv; ?>
                        
                        <div style="clear: both"></div>
                     </div> <!-- End .module-table-body -->
                </div> <!-- End .module -->
                
                
            
            <div style="clear:both;"></div>
            
            
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>   
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
$(document).ready(function(){
	$('.PagePhotoGallery').fancybox();
});
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>     